package com.jetinno.utils

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.media.AudioManager
import android.net.Uri
import android.view.SoundEffectConstants

/**
 * 获取APP相关信息
 * Created by zhangyuncai on 2021/9/6.
 */
object AppUtils {

    @JvmStatic
    fun getContext(): Context {
        return ContextHolder.getApplicationContext()
    }

    @JvmStatic
    fun getPackageName(): String {
        return getContext().packageName
    }

    @JvmStatic
    fun getVersionName(): String {
        try {
            val packageInfo = getContext().packageManager.getPackageInfo(getContext().packageName, 0)
            return packageInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return ""
    }

    @JvmStatic
    fun getVersionCode(): Long {
        try {
            val packageInfo = getContext().packageManager.getPackageInfo(getContext().packageName, 0)
            return packageInfo.versionCode.toLong()

        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 1
    }

    ///看门狗已安装的版本号
    @JvmStatic
    fun getJetinnoSystemVersionCode(): Int {
        try {
            val packageInfo = getContext().packageManager.getPackageInfo(HbConstant.JETINNO_SYSTEM_PACKAGE_NAME, 0)
            return packageInfo.versionCode

        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 0
    }

    @JvmStatic
    fun unstallApp(context: Context, pageName: String) {
        val intent = Intent()
        intent.action = Intent.ACTION_DELETE
        intent.setData(Uri.parse("package:${pageName}"))
        context.startActivity(intent)
    }

    /**
     * 播放点击的声音
     */
    @JvmStatic
    fun playClickSound(context: Context) {
        (context.getSystemService(Context.AUDIO_SERVICE) as AudioManager).playSoundEffect(SoundEffectConstants.CLICK)
    }

}